#include "view/main_window.h"

#include <QMessageBox>
#include <QtGui>
#include <iostream>

#include "utils/utils_time.h"

namespace project_template {

using namespace Qt;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowDesign) {
  ui->setupUi(this);
  this->setWindowIcon(QIcon(":/images/map_nav.jpg"));
  sequence_task_node_ = new SequenceTaskNode();
  image_visualization_node_ = new ImageVisualizationNode();

  rviz_visualization_ = new RvizVisualization(ui->layout_rviz_visual);
  RvizVisualizationInit();

  SignalSlotConnection();
}

MainWindow::~MainWindow() {
  delete ui;
  if (sequence_task_node_ != nullptr) {
    delete sequence_task_node_;
  }
  if (rviz_visualization_ != nullptr) {
    delete rviz_visualization_;
  }
  if (image_visualization_node_ != nullptr) {
    delete image_visualization_node_;
  }
}

void MainWindow::RvizVisualizationInit() {
  rviz_visualization_->SetFixedFrame("map");
  rviz_visualization_->DisplayGrid(10, 1, QColor(160, 160, 164), true);
  rviz_visualization_->DisplayAxes(true);
  rviz_visualization_->DisplayMarker(true);
  rviz_visualization_->DisplayMarkerArray(true);
  rviz_visualization_->DisplayLaserCloud(true);
}

void MainWindow::SignalSlotConnection() {
  connect(sequence_task_node_, SIGNAL(rosShutdown()), this, SLOT(close()));
  connect(image_visualization_node_, SIGNAL(rosShutdown()), this,
          SLOT(close()));
  connect(image_visualization_node_, SIGNAL(showRGBImage(QImage)), this,
          SLOT(SlotRGBImageShow(QImage)));
  connect(image_visualization_node_, SIGNAL(showDepthImage(QImage)), this,
          SLOT(SlotDepthImageShow(QImage)));
}

void MainWindow::SlotRGBImageShow(QImage _image) {
  ui->label_image_1->setPixmap(QPixmap::fromImage(_image).scaled(
      ui->label_image_1->width(), ui->label_image_1->height()));
}

void MainWindow::SlotDepthImageShow(QImage _image) {
  ui->label_image_2->setPixmap(QPixmap::fromImage(_image).scaled(
      ui->label_image_2->width(), ui->label_image_2->height()));
}

}  // namespace project_template
